package com.cyy.springbootinit.service.impl;
import java.util.Date;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyy.springbootinit.common.ErrorCode;
import com.cyy.springbootinit.constant.CommonConstant;
import com.cyy.springbootinit.exception.ThrowUtils;
import com.cyy.springbootinit.mapper.EquipmentInfoMapper;
import com.cyy.springbootinit.model.dto.equipmentinfo.EquipmentInfoQueryRequest;
import com.cyy.springbootinit.model.entity.EquipmentInfo;
import com.cyy.springbootinit.model.entity.User;
import com.cyy.springbootinit.model.vo.EquipmentInfoVO;
import com.cyy.springbootinit.model.vo.UserVO;
import com.cyy.springbootinit.service.EquipmentInfoService;
import com.cyy.springbootinit.service.UserService;
import com.cyy.springbootinit.utils.SqlUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 设备信息表服务实现
 *
 * @author 程序员cyy</a>
 * @from cyy</a>
 */
@Service
@Slf4j
public class EquipmentInfoServiceImpl extends ServiceImpl<EquipmentInfoMapper, EquipmentInfo> implements EquipmentInfoService {

    @Resource
    private UserService userService;

    /**
     * 校验数据
     *
     * @param equipmentInfo
     * @param add      对创建的数据进行校验
     */
    @Override
    public void validEquipmentInfo(EquipmentInfo equipmentInfo, boolean add) {

        ThrowUtils.throwIf(equipmentInfo == null, ErrorCode.PARAMS_ERROR);
        // todo 从对象中取值
        String equipmentName = equipmentInfo.getEquipmentName();
        String equipmentNumber = equipmentInfo.getEquipmentNumber();
        Integer dormitoryId = equipmentInfo.getDormitoryId();
        Integer typeName = equipmentInfo.getTypeName();
        // 创建数据时，参数不能为空
        if (add) {
            // todo 补充校验规则
            ThrowUtils.throwIf(StringUtils.isBlank(equipmentName), ErrorCode.PARAMS_ERROR);
            ThrowUtils.throwIf(dormitoryId==null, ErrorCode.PARAMS_ERROR);
        }
        // 修改数据时，有参数则校验
        // todo 补充校验规则
    }

    /**
     * 获取查询条件
     *
     * @param equipmentInfoQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<EquipmentInfo> getQueryWrapper(EquipmentInfoQueryRequest equipmentInfoQueryRequest) {
        QueryWrapper<EquipmentInfo> queryWrapper = new QueryWrapper<>();
        if (equipmentInfoQueryRequest == null) {
            return queryWrapper;
        }
        // todo 从对象中取值
        Integer equipmentId = equipmentInfoQueryRequest.getEquipmentId();
        String equipmentName = equipmentInfoQueryRequest.getEquipmentName();
        String equipmentNumber = equipmentInfoQueryRequest.getEquipmentNumber();
        Integer dormitoryId = equipmentInfoQueryRequest.getDormitoryId();
        Integer typeName = equipmentInfoQueryRequest.getTypeName();
        String searchText = equipmentInfoQueryRequest.getSearchText();
        String sortField = equipmentInfoQueryRequest.getSortField();
        String sortOrder = equipmentInfoQueryRequest.getSortOrder();
        // todo 补充需要的查询条件
        // 从多字段中搜索
        if (StringUtils.isNotBlank(searchText)) {
            // 需要拼接查询条件
            queryWrapper.and(qw -> qw.like("title", searchText).or().like("content", searchText));
        }
        // 模糊查询
        queryWrapper.like(StringUtils.isNotBlank(equipmentName), "equipmentName", equipmentName);
        queryWrapper.like(StringUtils.isNotBlank(equipmentNumber), "equipmentNumber", equipmentNumber);
        // JSON 数组查询
        // 精确查询
        queryWrapper.eq(ObjectUtils.isNotEmpty(equipmentId), "equipmentId", equipmentId);
        queryWrapper.eq(ObjectUtils.isNotEmpty(dormitoryId), "dormitoryId", dormitoryId);
        // 排序规则
        queryWrapper.orderBy(SqlUtils.validSortField(sortField),
                sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }

    /**
     * 获取设备信息表封装
     *
     * @param equipmentInfo
     * @param request
     * @return
     */
    @Override
    public EquipmentInfoVO getEquipmentInfoVO(EquipmentInfo equipmentInfo, HttpServletRequest request) {
        // 对象转封装类
        EquipmentInfoVO equipmentInfoVO = EquipmentInfoVO.objToVo(equipmentInfo);

        return equipmentInfoVO;
    }

    /**
     * 分页获取设备信息表封装
     *
     * @param equipmentInfoPage
     * @param request
     * @return
     */
    @Override
    public Page<EquipmentInfoVO> getEquipmentInfoVOPage(Page<EquipmentInfo> equipmentInfoPage, HttpServletRequest request) {
        List<EquipmentInfo> equipmentInfoList = equipmentInfoPage.getRecords();
        Page<EquipmentInfoVO> equipmentInfoVOPage = new Page<>(equipmentInfoPage.getCurrent(), equipmentInfoPage.getSize(), equipmentInfoPage.getTotal());
        if (CollUtil.isEmpty(equipmentInfoList)) {
            return equipmentInfoVOPage;
        }
        // 对象列表 => 封装对象列表
        List<EquipmentInfoVO> equipmentInfoVOList = equipmentInfoList.stream().map(equipmentInfo -> {
            return EquipmentInfoVO.objToVo(equipmentInfo);
        }).collect(Collectors.toList());
        equipmentInfoVOPage.setRecords(equipmentInfoVOList);
        return equipmentInfoVOPage;
    }

}
